package o;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.ActivityChooserView.ActivityChooserViewAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class bro {
    private static int ˊ;
    private static o.hg.b<Bitmap> ॱ = new o.hg.b(6);
    private d ʻ;
    private int ʻॱ;
    private int ʼ;
    private int ʽ;
    private int ˊॱ;
    private Context ˋ;
    private List<i> ˋॱ;
    private o.hg.c<a> ˎ = new o.hg.c(64);
    private o.hg.c<e> ˏ = new o.hg.c(64);
    private int ˏॱ;
    private int ͺ;
    private int ॱˊ;
    private int ॱˋ;
    private int ॱˎ;
    private brr ॱॱ = new brr();
    private String ॱᐝ;
    private g ᐝ;
    private int ᐝॱ;

    public interface g {
        void ˊ();

        void ˊ(int i, int i2);

        void ˊ(Exception exception);
    }

    static class a {
        i ˊ;
        b ˋ;
        Bitmap ˎ;
        Rect ˏ = new Rect();

        a() {
        }

        a(i iVar) {
            this.ˊ = iVar;
        }
    }

    static class b extends b {
        private volatile int ʻ;
        private g ˊ;
        private brp ˋ = this.ˏ.ˋ;
        private volatile int ˎ;
        private d ˏ;
        private volatile BitmapRegionDecoder ॱ;
        private volatile Exception ᐝ;

        b(d dVar, g gVar) {
            this.ˏ = dVar;
            this.ˊ = gVar;
        }

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected void ˏ() {
            try {
                this.ॱ = this.ˋ.ˊ();
                this.ˎ = this.ॱ.getWidth();
                this.ʻ = this.ॱ.getHeight();
            } catch (Exception e) {
                bqx.ˋ("ForumPostImageLoader", "doInBackground error");
                this.ᐝ = e;
            }
        }

        protected void onCancelled() {
            super.onCancelled();
            this.ˊ = null;
            this.ˋ = null;
            this.ˏ = null;
        }

        protected void ˊ() {
            super.ˊ();
            this.ˏ.ʼ = null;
            if (this.ᐝ == null) {
                this.ˏ.ᐝ = this.ˎ;
                this.ˏ.ʽ = this.ʻ;
                this.ˏ.ˊ = this.ॱ;
                this.ˊ.ˊ(this.ˎ, this.ʻ);
            } else {
                this.ˊ.ˊ(this.ᐝ);
            }
            this.ˊ = null;
            this.ˋ = null;
            this.ˏ = null;
        }
    }

    static class c extends b {
        private volatile Rect ʻ;
        private BitmapRegionDecoder ʼ;
        private volatile Throwable ʽ;
        private i ˊ;
        private a ˋ;
        private String ˋॱ;
        private int ˎ;
        private int ˏ;
        private int ॱ;
        private volatile Bitmap ॱॱ;
        private g ᐝ;

        c(i iVar, a aVar, int i, int i2, int i3, BitmapRegionDecoder bitmapRegionDecoder, g gVar, String str) {
            this.ˋ = aVar;
            this.ॱ = i;
            this.ˊ = iVar;
            this.ˎ = i2;
            this.ˏ = i3;
            this.ʼ = bitmapRegionDecoder;
            this.ᐝ = gVar;
            this.ˋॱ = str;
        }

        protected void onPreExecute() {
            super.onPreExecute();
        }

        protected void ˏ() {
            int ॱ = this.ॱ * bro.ˊ;
            int i = ॱ * this.ˊ.ˊ;
            int i2 = i + ॱ;
            int i3 = this.ˊ.ˏ * ॱ;
            ॱ += i3;
            if (i2 > this.ˎ) {
                i2 = this.ˎ;
            }
            if (ॱ > this.ˏ) {
                ॱ = this.ˏ;
            }
            this.ʻ = new Rect(i, i3, i2, ॱ);
            try {
                Options options = new Options();
                options.inBitmap = bro.ʼ();
                options.inMutable = true;
                options.inSampleSize = this.ॱ;
                this.ॱॱ = this.ʼ.decodeRegion(this.ʻ, options);
                this.ॱॱ = bsx.ˊ(this.ॱॱ, this.ˋॱ);
            } catch (Throwable e) {
                bqx.ˋ("ForumPostImageLoader", "LoadBlockTask doInBackground OutOfMemoryError");
                this.ʽ = e;
            } catch (Throwable e2) {
                this.ʽ = e2;
                bqx.ˋ("ForumPostImageLoader", "LoadBlockTask doInBackground error");
            }
        }

        protected void onCancelled() {
            super.onCancelled();
            if (this.ॱॱ != null) {
                bro.ॱ.ˋ(this.ॱॱ);
                this.ॱॱ = null;
            }
            this.ʼ = null;
            this.ˋ = null;
            this.ᐝ = null;
            this.ˊ = null;
        }

        protected void ˊ() {
            super.ˊ();
            this.ˋ.ˋ = null;
            if (this.ॱॱ != null) {
                this.ˋ.ˎ = this.ॱॱ;
                this.ˋ.ˏ.set(0, 0, this.ʻ.width() / this.ॱ, this.ʻ.height() / this.ॱ);
                if (this.ᐝ != null) {
                    this.ᐝ.ˊ();
                }
            }
            this.ʼ = null;
            this.ˋ = null;
            this.ᐝ = null;
            this.ˊ = null;
        }
    }

    static class d {
        private b ʼ;
        private int ʽ;
        private BitmapRegionDecoder ˊ;
        private brp ˋ;
        int ˎ;
        Map<i, a> ˏ;
        Map<i, a> ॱ;
        private int ᐝ;

        d(brp o_brp) {
            this.ˋ = o_brp;
        }
    }

    public static class e {
        public Rect ˊ = new Rect();
        public Rect ˎ = new Rect();
        public Bitmap ॱ;

        e() {
        }
    }

    static class i {
        int ˊ;
        int ˏ;

        i() {
        }

        i(int i, int i2) {
            this.ˏ = i;
            this.ˊ = i2;
        }

        i ॱ(int i, int i2) {
            this.ˏ = i;
            this.ˊ = i2;
            return this;
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof i)) {
                return false;
            }
            i iVar = (i) obj;
            if (this.ˏ == iVar.ˏ && this.ˊ == iVar.ˊ) {
                return true;
            }
            return false;
        }

        public int hashCode() {
            return (37 * (this.ˏ + 629)) + this.ˊ;
        }

        @NonNull
        public String toString() {
            return "row:" + this.ˏ + " col:" + this.ˊ;
        }
    }

    public bro(Context context) {
        this.ˋ = context;
        if (ˊ <= 0) {
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            ˋ(((displayMetrics.widthPixels + displayMetrics.heightPixels) % 4 == 0 ? 2 : 1) + ((displayMetrics.heightPixels + displayMetrics.widthPixels) / 4));
        }
    }

    static void ˋ(int i) {
        ˊ = i;
    }

    public boolean ˎ() {
        d dVar = this.ʻ;
        return (dVar == null || dVar.ˊ == null) ? false : true;
    }

    public void ˏ(g gVar) {
        this.ᐝ = gVar;
    }

    public void ˊ(brp o_brp) {
        if (this.ʻ != null) {
            ˋ(this.ʻ);
        }
        this.ʻ = new d(o_brp);
    }

    private void ˋ(d dVar) {
        ˋ(dVar.ʼ);
        dVar.ʼ = null;
        ˊ(dVar.ˏ);
        ˊ(dVar.ॱ);
    }

    public void ˏ() {
        if (this.ʻ != null) {
            ˋ(this.ʻ.ʼ);
            this.ʻ.ʼ = null;
            Map map = this.ʻ.ॱ;
            if (map != null) {
                for (a aVar : map.values()) {
                    ˋ(aVar.ˋ);
                    aVar.ˋ = null;
                }
            }
        }
    }

    public void ॱ(List<e> list, float f, Rect rect) {
        int i = 0;
        d dVar = this.ʻ;
        this.ॱˊ = ˏ(f);
        int i2 = dVar.ˎ;
        ˋ(dVar, list);
        int i3 = this.ॱˊ * ˊ;
        this.ˊॱ = ˊ * this.ॱˊ;
        int ˎ = (dVar.ʽ % this.ˊॱ == 0 ? 0 : 1) + (dVar.ʽ / this.ˊॱ);
        int ˋ = dVar.ᐝ / this.ˊॱ;
        if (dVar.ᐝ % this.ˊॱ != 0) {
            i = 1;
        }
        i += ˋ;
        ˊ(rect, ˎ, i);
        this.ʻॱ = this.ʽ;
        this.ᐝॱ = this.ʼ;
        this.ॱˎ = this.ˏॱ;
        this.ॱˋ = this.ͺ;
        if ((this.ͺ - this.ˏॱ) * (this.ʼ - this.ʽ) <= 16) {
            ˊ(rect);
            if (this.ᐝॱ > ˎ) {
                this.ᐝॱ = ˎ;
            }
            if (this.ॱˋ > i) {
                this.ॱˋ = i;
            }
        }
        ˊ(dVar, i2);
        i iVar = new i();
        List arrayList = new ArrayList();
        Map map = dVar.ॱ;
        ˊ(dVar, iVar, map, arrayList, i3);
        ॱ(dVar, iVar, map, list, arrayList);
    }

    private void ˋ(d dVar, List<e> list) {
        for (e eVar : list) {
            eVar.ॱ = null;
            this.ˏ.ˋ(eVar);
        }
        list.clear();
        if (dVar.ˊ == null && ˎ(dVar.ʼ)) {
            dVar.ʼ = new b(dVar, this.ᐝ);
            ˊ(dVar.ʼ);
        }
    }

    private void ˊ(Rect rect, int i, int i2) {
        int i3 = 1;
        this.ʽ = rect.top / this.ˊॱ;
        this.ʼ = (rect.bottom % this.ˊॱ == 0 ? 0 : 1) + (rect.bottom / this.ˊॱ);
        this.ˏॱ = rect.left / this.ˊॱ;
        int i4 = rect.right / this.ˊॱ;
        if (rect.right % this.ˊॱ == 0) {
            i3 = 0;
        }
        this.ͺ = i4 + i3;
        if (this.ʽ < 0) {
            this.ʽ = 0;
        }
        if (this.ˏॱ < 0) {
            this.ˏॱ = 0;
        }
        if (this.ʼ > i) {
            this.ʼ = i;
        }
        if (this.ͺ > i2) {
            this.ͺ = i2;
        }
    }

    private void ˊ(Rect rect) {
        int i;
        int i2 = this.ˊॱ - (rect.top % this.ˊॱ);
        int i3 = rect.bottom % this.ˊॱ;
        if (this.ͺ - this.ˏॱ == 1) {
            i = 0;
        } else if (this.ͺ - this.ˏॱ == 2) {
            i = this.ˊॱ / 2;
        } else if (this.ͺ - this.ˏॱ == 3) {
            i = (this.ˊॱ / 8) * 7;
        } else {
            i = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        }
        if (i2 > i3) {
            if (i2 > i) {
                this.ʻॱ++;
            }
        } else if (i3 > i) {
            this.ᐝॱ++;
        }
        ॱ(rect);
    }

    private void ॱ(Rect rect) {
        int i;
        int i2 = this.ˊॱ - (rect.left % this.ˊॱ);
        int i3 = rect.right % this.ˊॱ;
        if (this.ʼ - this.ʽ == 1) {
            i = 0;
        } else if (this.ʼ - this.ʽ == 2) {
            i = this.ˊॱ / 2;
        } else if (this.ʼ - this.ʽ == 3) {
            i = (this.ˊॱ / 8) * 7;
        } else {
            i = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
        }
        if (i2 > i3) {
            if (i2 > i) {
                this.ॱˎ++;
            }
        } else if (i3 > i) {
            this.ॱˎ++;
        }
        if (this.ʻॱ < 0) {
            this.ʻॱ = 0;
        }
        if (this.ॱˎ < 0) {
            this.ॱˎ = 0;
        }
    }

    private void ˊ(d dVar, int i) {
        this.ˋॱ = new LinkedList();
        if (!(dVar.ॱ == null || i == this.ॱˊ)) {
            Map map = dVar.ˏ;
            Map map2 = dVar.ॱ;
            if (this.ॱˊ == i * 2) {
                dVar.ॱ = map;
                ˊ(map2);
                dVar.ˏ = map2;
            } else if (this.ॱˊ == i / 2) {
                dVar.ˏ = map2;
                ˊ(map);
                dVar.ॱ = map;
            } else {
                ˊ(map);
                ˊ(map2);
            }
        }
        dVar.ˎ = this.ॱˊ;
        if (dVar.ॱ == null) {
            dVar.ॱ = new HashMap();
        }
    }

    private void ˊ(d dVar, i iVar, Map<i, a> map, List<e> list, int i) {
        dVar.ॱ = new HashMap();
        for (int i2 = this.ʽ; i2 < this.ʼ; i2++) {
            for (int i3 = this.ˏॱ; i3 < this.ͺ; i3++) {
                iVar.ॱ(i2, i3);
                a ˊ = ˊ(iVar, (a) map.get(iVar), dVar.ॱ, this.ॱˊ, dVar.ᐝ, dVar.ʽ, dVar.ˊ);
                if (ˊ.ˎ != null) {
                    e eVar = (e) this.ˏ.ˎ();
                    if (eVar == null) {
                        eVar = new e();
                    }
                    Rect rect = eVar.ˊ;
                    rect.left = i3 * i;
                    rect.top = i2 * i;
                    rect.right = rect.left + (ˊ.ˏ.width() * this.ॱˊ);
                    rect.bottom = rect.top + (ˊ.ˏ.height() * this.ॱˊ);
                    eVar.ˎ.set(0, 0, ˊ.ˏ.width(), ˊ.ˏ.height());
                    eVar.ॱ = ˊ.ˎ;
                    list.add(eVar);
                } else {
                    this.ˋॱ.add(new i(i2, i3));
                }
            }
        }
    }

    private void ॱ(d dVar, i iVar, Map<i, a> map, List<e> list, List<e> list2) {
        int i;
        int i2;
        for (i = this.ʻॱ; i < this.ʽ; i++) {
            for (i2 = this.ॱˎ; i2 < this.ॱˋ; i2++) {
                iVar.ॱ(i, i2);
                ˊ(iVar, (a) map.get(iVar), dVar.ॱ, this.ॱˊ, dVar.ᐝ, dVar.ʽ, dVar.ˊ);
            }
        }
        for (i = this.ʼ; i < this.ᐝॱ; i++) {
            for (i2 = this.ॱˎ; i2 < this.ॱˋ; i2++) {
                iVar.ॱ(i, i2);
                ˊ(iVar, (a) map.get(iVar), dVar.ॱ, this.ॱˊ, dVar.ᐝ, dVar.ʽ, dVar.ˊ);
            }
        }
        for (i = this.ʽ; i < this.ʼ; i++) {
            for (i2 = this.ॱˎ; i2 < this.ˏॱ; i2++) {
                iVar.ॱ(i, i2);
                ˊ(iVar, (a) map.get(iVar), dVar.ॱ, this.ॱˊ, dVar.ᐝ, dVar.ʽ, dVar.ˊ);
            }
        }
        for (i = this.ʽ; i < this.ʼ; i++) {
            for (i2 = this.ͺ; i2 < this.ॱˋ; i2++) {
                iVar.ॱ(i, i2);
                ˊ(iVar, (a) map.get(iVar), dVar.ॱ, this.ॱˊ, dVar.ᐝ, dVar.ʽ, dVar.ˊ);
            }
        }
        map.keySet().removeAll(dVar.ॱ.keySet());
        ˊ((Map) map);
        list.addAll(ˏ(dVar, this.ॱˊ, this.ˋॱ));
        list.addAll(list2);
    }

    private List<e> ˏ(d dVar, int i, List<i> list) {
        List<e> arrayList = new ArrayList();
        i iVar = new i();
        if (!(dVar.ˏ == null || dVar.ˏ.isEmpty())) {
            int i2 = (i * 2) / i;
            Iterator it = dVar.ˏ.entrySet().iterator();
            while (it.hasNext()) {
                Entry entry = (Entry) it.next();
                ˋ(((a) entry.getValue()).ˋ);
                dVar.ʼ = null;
                if (!list.isEmpty()) {
                    ˎ(entry, i2, i, it, arrayList, iVar);
                }
            }
        }
        return arrayList;
    }

    private void ˎ(Entry<i, a> entry, int i, int i2, Iterator<Entry<i, a>> it, List<e> list, i iVar) {
        a aVar = (a) entry.getValue();
        i iVar2 = (i) entry.getKey();
        if (aVar.ˎ == null || iVar2.ˏ < this.ʽ / 2 || iVar2.ˏ > this.ʼ / 2 || iVar2.ˊ < this.ˏॱ / 2 || iVar2.ˊ > this.ͺ / 2) {
            it.remove();
            ˋ(aVar);
            return;
        }
        int i3 = iVar2.ˏ * i;
        int i4 = iVar2.ˊ * i;
        int width = aVar.ˏ.width();
        int height = aVar.ˏ.height();
        int ceil = (int) Math.ceil((double) ((1.0f * ((float) ˊ)) / ((float) i)));
        int i5 = 0;
        int i6 = i3;
        while (i6 < i3 + i) {
            int i7 = i5 * ceil;
            if (i7 < height) {
                int i8 = 0;
                for (int i9 = i4; i9 < i4 + i; i9++) {
                    int i10 = i8 * ceil;
                    if (i10 >= width) {
                        break;
                    }
                    if (this.ˋॱ.remove(iVar.ॱ(i6, i9))) {
                        int i11;
                        int i12 = i10 + ceil;
                        int i13 = i7 + ceil;
                        if (i12 > width) {
                            i11 = width;
                        } else {
                            i11 = i12;
                        }
                        if (i13 > height) {
                            i12 = height;
                        } else {
                            i12 = i13;
                        }
                        e eVar = (e) this.ˏ.ˎ();
                        if (this.ˏ.ˎ() == null) {
                            eVar = new e();
                        }
                        eVar.ॱ = aVar.ˎ;
                        Rect rect = eVar.ˊ;
                        rect.left = (i9 * i2) * ˊ;
                        rect.top = (i6 * i2) * ˊ;
                        rect.right = rect.left + (((i11 - i10) * i2) * 2);
                        rect.bottom = rect.top + (((i12 - i7) * i2) * 2);
                        eVar.ˎ.set(i10, i7, i11, i12);
                        eVar.ॱ = aVar.ˎ;
                        list.add(eVar);
                    }
                    i8++;
                }
                i5++;
                i6++;
            } else {
                return;
            }
        }
    }

    private boolean ˎ(b bVar) {
        return bVar == null;
    }

    private void ˊ(b bVar) {
        this.ॱॱ.ˋ(bVar);
    }

    private void ˋ(b bVar) {
        if (bVar != null) {
            this.ॱॱ.ˎ(bVar);
        }
    }

    private void ˊ(Map<i, a> map) {
        if (map != null) {
            for (Entry value : map.entrySet()) {
                ˋ((a) value.getValue());
            }
            map.clear();
        }
    }

    private void ˋ(a aVar) {
        ˋ(aVar.ˋ);
        aVar.ˋ = null;
        if (aVar.ˎ != null) {
            ॱ.ˋ(aVar.ˎ);
            aVar.ˎ = null;
        }
        this.ˎ.ˋ(aVar);
    }

    private a ˊ(i iVar, a aVar, Map<i, a> map, int i, int i2, int i3, BitmapRegionDecoder bitmapRegionDecoder) {
        a aVar2;
        if (aVar == null) {
            a aVar3 = (a) this.ˎ.ˎ();
            if (aVar3 == null) {
                aVar2 = new a(new i(iVar.ˏ, iVar.ˊ));
            } else if (aVar3.ˊ == null) {
                aVar3.ˊ = new i(iVar.ˏ, iVar.ˊ);
                aVar2 = aVar3;
            } else {
                aVar3.ˊ.ॱ(iVar.ˏ, iVar.ˊ);
                aVar2 = aVar3;
            }
        } else {
            aVar2 = aVar;
        }
        if (aVar2.ˎ == null && ˎ(aVar2.ˋ)) {
            aVar2.ˋ = new c(aVar2.ˊ, aVar2, i, i2, i3, bitmapRegionDecoder, this.ᐝ, this.ॱᐝ);
            ˊ(aVar2.ˋ);
        }
        map.put(aVar2.ˊ, aVar2);
        return aVar2;
    }

    private int ˏ(float f) {
        return ॱ(Math.round(f));
    }

    private int ॱ(int i) {
        int i2 = 1;
        while (i2 < i) {
            i2 *= 2;
        }
        return i2;
    }

    public int ˋ() {
        if (this.ʻ == null) {
            return 0;
        }
        return this.ʻ.ᐝ;
    }

    public int ˊ() {
        if (this.ʻ == null) {
            return 0;
        }
        return this.ʻ.ʽ;
    }

    public void ˊ(String str) {
        this.ॱᐝ = str;
    }

    private static Bitmap ʼ() {
        Bitmap bitmap = (Bitmap) ॱ.ˎ();
        if (bitmap == null) {
            return Bitmap.createBitmap(ˊ, ˊ, Config.ARGB_8888);
        }
        return bitmap;
    }
}
